package models

import (
	"strings"
	"time"

	"gitee.com/sansaniot/ssiot-core/httpmvc/model"

	dto "ssadmin/common/query"
	"ssadmin/internal/admin/models/query"
)

type Resource struct {
	Id         string     `json:"id" gorm:"primaryKey"`
	Path       string     `json:"path"`
	FileSize   int64      `json:"fileSize"`
	FileName   string     `json:"fileName"`
	Crc16      int64      `json:"crc16"`
	Remark     string     `json:"remark"`
	FileType   int        `json:"fileType"`
	Md5        string     `json:"md5"`
	Flag       int        `json:"flag"`
	ExtType    string     `json:"extType"`
	Width      float64    `json:"width"`
	Height     float64    `json:"height"`
	AgencyId   string     `json:"agencyId"`
	CreatorId  *string    `json:"creatorId"`
	Creator    *string    `json:"creator"`
	CreateTime *time.Time `json:"createTime"`
	UpdateTime *time.Time `json:"updateTime"`

	model.Model
}

func (Resource) TableName() string {
	return "sys_resource"
}

func (e *Resource) Generate() *Resource {
	o := *e
	return &o
}

func (e *Resource) GetId() string {
	return e.Id
}

func (e *Resource) AddResource(items []Resource, force bool) (string, error) {
	if len(items) == 0 {
		return "", nil
	}
	paths := make([]string, 0)
	for _, v := range items {
		paths = append(paths, v.Path)
	}
	if force {
		_ = e.Delete(e, dto.MakeCondition(query.ItemQuery{PathIn: paths}))
	}
	if err := e.Add(&items); err != nil {
		return "", err
	}
	return strings.Join(paths, ","), nil
}
